//---------------------------------------------------------------------
// Copyright (C) 2010 - Cobalt Solution / Sebastien Leon              |
//---------------------------------------------------------------------
// This software is under dual licence: commercial licence + GPL v3   |
// To keep closed your modification of this software, you need to     |
// contact me to obtain a commercial licence.                         |
// Any other derivated work should remain Open Source.                |
// Contact me for a licence, a question or if you want to commit      |
// your code to the main tree: seb at cobaltsolutions dot info        |
//---------------------------------------------------------------------

#include "EditPref.h"
#include "Core.h"
#include <QColorDialog>
#include <QFileDialog>

EditPref::EditPref(QWidget* parent, Core& core, bool readOnly):QDialog(parent),
  mr_core(core)
{
  ui.setupUi(this);

  // Install pixmaps:
  ui.mp_path->setIcon(QPixmap(":/directory.png"));

  // lock UI:
  ui.mp_bkgColor  ->setEnabled(!readOnly);
  ui.mp_path      ->setEnabled(!readOnly);
  ui.mp_monitoring->setEnabled(!readOnly);
  ui.mp_reporting ->setEnabled(!readOnly);

  UpdateGui();
}
//----------------------------------------------------
void EditPref::UpdateGui()
{
  ui.mp_infoLicence->setText   (mr_core.IsValidLicence() ? "Maintenance en cours..." : "Invalide ou non verifiable");
  ui.mp_monitoring ->setChecked(mr_core.Monitoring());
  ui.mp_reporting  ->setChecked(mr_core.Reporting ());
  ui.mp_pathLine   ->setText   (mr_core.Path      ());
  UpdateColor();
}
//----------------------------------------------------
void EditPref::UpdateColor()
{
  QPixmap color(25,25);
  color.fill(mr_core.Color());
  ui.mp_bkgColor->setIcon(color);
}
//----------------------------------------------------
void EditPref::monitoringChanged()
{
  mr_core.ChangeMonitoring(ui.mp_monitoring->isChecked());
}
//----------------------------------------------------
void EditPref::reportingChanged ()
{
  mr_core.ChangeReporting(ui.mp_reporting->isChecked());
}
//----------------------------------------------------
void EditPref::selectColor()
{
  QColor c = QColorDialog::getColor ( mr_core.Color(), this, "Couleur du fond");
  if (c.isValid())
  {
    mr_core.ChangeBkgColor(c);
    UpdateColor();
  }
}
//----------------------------------------------------
void EditPref::selectPath()
{
  QString dir = QFileDialog::getExistingDirectory(this, "Choix du chemin des dessins", mr_core.Path(), QFileDialog::ShowDirsOnly);
  if (!dir.isEmpty())
  {
    mr_core.ChangePath(dir);
    ui.mp_pathLine->setText(dir);
  }
}
//----------------------------------------------------
